package com.tgy.myplus;

import cn.hutool.core.lang.copier.Copier;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.tgy.myplus.dao.StudentDao;
import com.tgy.myplus.entity.Student;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.io.Console;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

@SpringBootTest

public class MybatisPlusCRUD {

    @Resource
    StudentDao studentDao;


    /**
     * QueryWrapper 查询的使用
     */
    @Test
    public void testQueryWrapper() {
        // Step1：创建一个 QueryWrapper 对象
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();

        // Step2： 构造查询条件
        queryWrapper
                .select("*")
                .lt("age", 12)//小于23岁
                .like("name", "小")//name包括i字母
                .or().ge("age",18);//或者年龄大于18

        // Step3：执行查询
        studentDao
                .selectList(queryWrapper)
                .forEach(System.out::println);
    }


    /**
     * UpdateWrapper 查询的使用
     */
    @Test
    public void testUpdateWrapper() {
        int reduceAge = 2;
        Student student = new Student(1);

/*        // 更新用户数据的wrapper
        UpdateWrapper<Student> updateWrapper = new UpdateWrapper<Student>();
        //修改数据的语句
        updateWrapper.set("name", "jack");//使name字段的值更新为“jack”
        updateWrapper.setSql("age = age - " + reduceAge);//自定义的sql语句  年龄减2
        //条件
        updateWrapper.eq("id", 12);//修改数据的id
        studentDao.update(student, updateWrapper);*/


        Student student1 = new Student();
        student1.setId(3);
        student1.setName("updById1");
        student1.setAge(6);
        System.out.println(studentDao.updateById(student1));

        System.out.println(studentDao.updateById(new Student(3, 3, 4, null, null, null, null)));


    }


    @Test
    public void testLambdatWrapper() {
//        System.out.println(
//                studentDao.selectList(
//                new QueryWrapper<Student>()
//                        .lambda()
//                        .eq(Student::getName,"小唐"))
//        );
        System.out.println(
                studentDao.selectList(
                        new QueryWrapper<Student>()
                                .lambda()
                                .eq(Student::getAge,1))
        );
/*        System.out.println(
                studentDao.selectList(
                        new QueryWrapper<Student>()
                                .lambda()
                                .ge(Student ->Student.getAge(),1))
        );*/

        System.out.println(
                studentDao.selectList(
                        new QueryWrapper<Student>()
                                .lambda()
                                .isNotNull(Student::getBirth))
        );

        System.out.println(studentDao.selectList(new LambdaQueryWrapper<Student>().eq(true, Student::getName, "小唐")));


        /*
        只能查一个
         */
        Student one = new LambdaQueryChainWrapper<>(studentDao)
                       .eq(Student::getName, "梅西")
                         .one();
                 System.out.println("UserOne:" + one);

        List<Student> list = new LambdaQueryChainWrapper<>(studentDao)
                .eq(Student::getName, "小唐")
                .list();
        System.out.println("UserOne:" + one);


    }

    /**
     * 输出list
     */
    @Test
    public void test() {
        String[] array = {"aaaa", "bbbb", "cccc"};
        List<String> list = Arrays.asList(array);

        //Java 7
        for(String s:list){
            System.out.println(s);
        }

        //Java 8

        list.forEach(System.out::println);
    }


    @Test
    public void testLambdaQueryWrapper() {
        // Step1：创建一个 QueryWrapper 对象
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();


        studentDao
                .selectList(new QueryWrapper<Student>().lambda().eq(true, Student::getSex, 0))
                .forEach(System.out::println);

        System.out.println(new LambdaQueryChainWrapper<>(studentDao)
                .eq(Student::getName, "小唐")
                .list());

        studentDao
                .selectList(new QueryWrapper<Student>().select("*").lt("age",12).like("name","小"))
                .forEach(System.out::println);

        System.out.println(studentDao
                .update(new Student(), new UpdateWrapper<Student>().set("name", "吸罗").set("age", 35).eq("name", "4534")));



    }





}
